package com.gepengjun.lims.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.gepengjun.lims.entity.Permission;
import com.gepengjun.lims.entity.RefPermissionRole;
import com.gepengjun.lims.entity.Role;
import com.gepengjun.lims.mapper.RoleMapper;
import com.gepengjun.lims.pojo.Node;
import com.gepengjun.lims.service.PermissionService;
import com.gepengjun.lims.service.RefPermissionRoleService;
import com.gepengjun.lims.service.RoleService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author jun
 * @since 2018-08-19
 */
@Service
public class RoleServiceImpl extends ServiceImpl<RoleMapper , Role> implements RoleService{

    @Resource
    private  RoleMapper roleMapper;

    @Autowired
    private PermissionService permissionService;

    @Autowired
    private RefPermissionRoleService refPermissionRoleService;

    @Override
    @Transactional
    @CacheEvict(value = "role",allEntries = true)
    public boolean insert(Role var1){ return super.insert(var1); }

    @Override
    @Transactional
    @CacheEvict(value = "role",allEntries = true)
    public boolean insertAllColumn(Role var1){ return super.insertAllColumn(var1);}

    @Override
    @Transactional
    @CacheEvict(value = "role",allEntries = true)
    public boolean insertBatch(List<Role> var1){ return super.insertBatch(var1);}

    @Override
    @Transactional
    @CacheEvict(value = "role",allEntries = true)
    public boolean insertBatch(List<Role> var1, int var2){ return super.insertBatch(var1,var2);}

    @Override
    @Transactional
    @CacheEvict(value = "role",allEntries = true)
    public boolean insertOrUpdateBatch(List<Role> var1){ return super.insertOrUpdateBatch(var1);}

    @Override
    @Transactional
    @CacheEvict(value = "role",allEntries = true)
    public boolean insertOrUpdateBatch(List<Role> var1, int var2){ return super.insertOrUpdateBatch(var1,var2);}

    @Override
    @Transactional
    @CacheEvict(value = "role",allEntries = true)
    public boolean insertOrUpdateAllColumnBatch(List<Role> var1){ return super.insertOrUpdateAllColumnBatch(var1);}

    @Override
    @Transactional
    @CacheEvict(value = "role",allEntries = true)
    public boolean insertOrUpdateAllColumnBatch(List<Role> var1, int var2){ return super.insertOrUpdateAllColumnBatch(var1,var2);}

    @Override
    @Transactional
    @CacheEvict(value = "role",allEntries = true)
    public boolean deleteById(Serializable var1){ return super.deleteById(var1);}

    @Override
    @Transactional
    @CacheEvict(value = "role",allEntries = true)
    public boolean deleteByMap(Map<String, Object> var1){ return super.deleteByMap(var1);}

    @Override
    @Transactional
    @CacheEvict(value = "role",allEntries = true)
    public boolean delete(Wrapper<Role> var1){ return super.delete(var1);}

    @Override
    @Transactional
    @CacheEvict(value = "role",allEntries = true)
    public boolean deleteBatchIds(Collection<? extends Serializable> var1){ return super.deleteBatchIds(var1);}

    @Override
    @Transactional
    @CacheEvict(value = "role",allEntries = true)
    public boolean updateById(Role var1){ return super.updateById(var1);}

    @Override
    @Transactional
    @CacheEvict(value = "role",allEntries = true)
    public boolean updateAllColumnById(Role var1){ return super.updateAllColumnById(var1);}

    @Override
    @Transactional
    @CacheEvict(value = "role",allEntries = true)
    public boolean update(Role var1, Wrapper<Role> var2){ return super.update(var1,var2);}

    @Override
    @Transactional
    @CacheEvict(value = "role",allEntries = true)
    public boolean updateForSet(String var1, Wrapper<Role> var2){ return super.updateForSet(var1,var2);}

    @Override
    @Transactional
    @CacheEvict(value = "role",allEntries = true)
    public boolean updateBatchById(List<Role> var1){ return super.updateBatchById(var1);}

    @Override
    @Transactional
    @CacheEvict(value = "role",allEntries = true)
    public boolean updateBatchById(List<Role> var1, int var2){ return super.updateBatchById(var1,var2);}

    @Override
    @Transactional
    @CacheEvict(value = "role",allEntries = true)
    public boolean updateAllColumnBatchById(List<Role> var1){ return super.updateAllColumnBatchById(var1);}

    @Override
    @Transactional
    @CacheEvict(value = "role",allEntries = true)
    public boolean updateAllColumnBatchById(List<Role> var1, int var2){ return super.updateAllColumnBatchById(var1,var2);}

    @Override
    @Transactional
    @CacheEvict(value = "role",allEntries = true)
    public boolean insertOrUpdate(Role var1){ return super.insertOrUpdate(var1);}

    @Override
    @Transactional
    @CacheEvict(value = "role",allEntries = true)
    public boolean insertOrUpdateAllColumn(Role var1){ return super.insertOrUpdateAllColumn(var1);}

    @Override
    @Cacheable(value = "role",keyGenerator = "myKeyGenerator")
    public Role selectById(Serializable var1){ return super.selectById(var1);}

    @Override
    @Cacheable(value = "role",keyGenerator = "myKeyGenerator")
    public List<Role> selectBatchIds(Collection<? extends Serializable> var1){ return super.selectBatchIds(var1);}

    @Override
    @Cacheable(value = "role",keyGenerator = "myKeyGenerator")
    public List<Role> selectByMap(Map<String, Object> var1){ return super.selectByMap(var1);}

    @Override
    @Cacheable(value = "role",keyGenerator = "myKeyGenerator")
    public Role selectOne(Wrapper<Role> var1){ return super.selectOne(var1);}

    @Override
    @Cacheable(value = "role",keyGenerator = "myKeyGenerator")
    public Map<String, Object> selectMap(Wrapper<Role> var1){ return super.selectMap(var1);}

    @Override
    @Cacheable(value = "role",keyGenerator = "myKeyGenerator")
    public Object selectObj(Wrapper<Role> var1){ return super.selectObj(var1);}

    @Override
    @Cacheable(value = "role",keyGenerator = "myKeyGenerator")
    public int selectCount(Wrapper<Role> var1){ return super.selectCount(var1);}

    @Override
    @Cacheable(value = "role",keyGenerator = "myKeyGenerator")
    public List<Role> selectList(Wrapper<Role> var1){ return super.selectList(var1);}

    @Override
    @Cacheable(value = "role",keyGenerator = "myKeyGenerator")
    public Page<Role> selectPage(Page<Role> var1){ return super.selectPage(var1);}

    @Override
    @Cacheable(value = "role",keyGenerator = "myKeyGenerator")
    public List<Map<String, Object>> selectMaps(Wrapper<Role> var1){ return super.selectMaps(var1);}

    @Override
    @Cacheable(value = "role",keyGenerator = "myKeyGenerator")
    public List<Object> selectObjs(Wrapper<Role> var1){ return super.selectObjs(var1);}

    @Override
    @Cacheable(value = "role",keyGenerator = "myKeyGenerator")
    public Page<Map<String, Object>> selectMapsPage(Page var1, Wrapper<Role> var2){ return super.selectMapsPage(var1,var2);}

    @Override
    @Cacheable(value = "role",keyGenerator = "myKeyGenerator")
    public Page<Role> selectPage(Page<Role> var1, Wrapper<Role> var2){ return super.selectPage(var1,var2);}




    @Override
    @Cacheable(value = "role",keyGenerator = "myKeyGenerator")
    public List<Role> findAllAvailableRole() {
        EntityWrapper<Role> entityWrapper = new EntityWrapper<>();
        entityWrapper.eq("available",true);
        return roleMapper.selectList(entityWrapper);
    }

    @Override
    @Cacheable(value = "role",keyGenerator = "myKeyGenerator")
    public List<Role> findRolesByUserId(Long userId) {
        return roleMapper.selectRolesByUserId(userId);
    }

    @Override
    @Cacheable(value = "role",keyGenerator = "myKeyGenerator")
    public Page<Role> getRolePage(Page<Role> page, Role role) {
        EntityWrapper<Role> wrapper = new EntityWrapper<>();
        if (role != null && StringUtils.isNotEmpty(role.getRoleName())){
            wrapper.like("role_name",role.getRoleName());
        }
        page = selectPage(page,wrapper);

        return page;
    }

    @Override
    @CacheEvict(value = "role",allEntries = true)
    @Transactional
    public Boolean batchDisableUsers(List<Long> roleList) {
        if (roleList != null && roleList.size() > 0){
            Role role = new Role();
            role.setAvailable(false);
            EntityWrapper<Role> wrapper = new EntityWrapper<>();
            wrapper.in("role_id",roleList);
            return super.update(role,wrapper);
        }
        return false;
    }

    @Override
    @CacheEvict(value = "role",allEntries = true)
    @Transactional
    public Boolean batchAbleRoles(List<Long> roleList) {
        if (roleList != null && roleList.size() > 0){
            Role role = new Role();
            role.setAvailable(true);
            EntityWrapper<Role> wrapper = new EntityWrapper<>();
            wrapper.in("role_id",roleList);
            return super.update(role,wrapper);
        }
        return false;
    }

    @Override
    public List<Node> gerRolePermissions(Long roleId) {
        if (roleId != null){
            List<Node> nodeList = new ArrayList<>();
            List<Permission> permissionList = permissionService.findAllAvaiablePermissions();
            List<RefPermissionRole> rprList = refPermissionRoleService.selectByRoleId(roleId);

            if (permissionList != null && permissionList.size() > 0){
                for (Permission permission:permissionList){
                    Node node = new Node();
                    node.setId(permission.getPermissionId());
                    node.setName(permission.getPermissionName());
                    node.setpId(permission.getParentId());
                    if (rprList != null && rprList.size() > 0){
                        for (RefPermissionRole refPermissionRole:rprList){
                            if (permission.getPermissionId() == refPermissionRole.getPermissionId()){
                                node.setChecked(true);
                            }
                        }
                    }
                    nodeList.add(node);
                }
                return nodeList;
            }
        }
        return null;
    }
}
